package com.idcard.readcard.utils;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.provider.Settings;

public class NfcUtils {

    //nfc
    public NfcAdapter mNfcAdapter;
    public IntentFilter[] mIntentFilter = null;
    public PendingIntent mPendingIntent = null;
    public String[][] mTechList = null;

    /**
     * 构造函数，用于初始化nfc
     */
    public NfcUtils(Activity activity) {
        mNfcAdapter = nfcCheck(activity);
        nfcInit(activity);
    }


    //2.检测NFC的检测函数
    private NfcAdapter nfcCheck(Context context) {


        NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
        //3.获取nfc适配器
        if (mNfcAdapter == null) {
            //3.如果获取的mNfcAdapter=null，则说明该手机不支持nfc功能
            return null;
        } else {
            //4.如果手机有nfc功能，进一步判断nfc是否打开
            if (!mNfcAdapter.isEnabled()) {
                //5.假如手机的nfc功能没有被打开。则跳到打开nfc功能的界面
                Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS);
                context.startActivity(setNfc);
            }
        }
        return mNfcAdapter;
    }


    /**
     * 初始化nfc设置
     */
    public void nfcInit(Activity activity) {
        mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        try {
            filter.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            e.printStackTrace();
        }
        mIntentFilter = new IntentFilter[]{filter, filter2};
        mTechList = null;
    }

}
